import service from "../common/service";

//获取字典名称
const getBcmDictName = (className:string, value:any) => {
	const bcmDict = service.getLocalStorage('bcmDict');
	// console.log(bcmDict,'bcmDictbcmDictbcmDict')
	if (!bcmDict) {
		return '';
	}
	const dictMap = bcmDict[className]?.dataItems.find((x:any) => x.value == value);
	return dictMap ? dictMap.text : value
}

const getBcmDictItem = (className:string, value:any) => {
	const bcmDict = service.getLocalStorage('bcmDict');
	// console.log(bcmDict,'bcmDictbcmDictbcmDict')
	if (!bcmDict) {
		return '';
	}
	const dictMap = bcmDict[className]?.dataItems.find((x:any) => x.value == value);
	return dictMap ? dictMap : ''
}

//获取字典的集合
const getBcmDictList = (className:string) => {
	const bcmDict = service.getLocalStorage('bcmDict')
	if (!bcmDict) {
		return [];
	}
	const dictMap = bcmDict[className]?.dataItems;
	return dictMap
}
const getBcmDictLists = (classNames:string[]) => {
	const bcmDict = service.getLocalStorage('bcmDict');
	if (!bcmDict) {
		return [];
	}
	const dictLists:any = {};
	classNames.forEach((value,i) => {
		if( bcmDict.hasOwnProperty(value) ){
			dictLists[value] = bcmDict[value]?.dataItems;
		}
	})
	return dictLists;
}

const formatPrice = (number:any, type = undefined) => {
	if (type) {
	  return number > 0 ? `-￥${number.toFixed(2)}` : '￥0.00'
	} else {
	  return number > 0 ? `￥${number.toFixed(2)}` : '￥0.00'
	}
}

const formatTimeRange = (startTime, endTime) => {
	let end, start;
	if(!!endTime){
		end = new Date(endTime.replace(/-/g, "/"));
	}
	if(!!startTime){
		start = new Date(startTime.replace(/-/g, "/"));
	}
	const now = new Date();
	if(start.getFullYear() === now.getFullYear()){
		//与当前同年
		if(start.toDateString() === end.toDateString()){
			//同一天不同时间
			return `${start.getMonth() + 1}月${start.getDate()}日 ${start.getHours().toString().padStart(2, '0')}:${start.getMinutes().toString().padStart(2, '0')} - ${end.getHours().toString().padStart(2, '0')}:${end.getMinutes().toString().padStart(2, '0')}`;
		}else{
			return `${start.getMonth() + 1}月${start.getDate()}日 ${start.getHours().toString().padStart(2, '0')}:${start.getMinutes().toString().padStart(2, '0')} - ${end.getMonth() + 1}月${end.getDate()}日 ${end.getHours().toString().padStart(2, '0')}:${end.getMinutes().toString().padStart(2, '0')}`;
		}
	}else{
		//与当前不同年
		if(start.toDateString() === end.toDateString()){
			return `${start.getFullYear()}年${start.getMonth() + 1}月${start.getDate()}日 ${start.getHours().toString().padStart(2, '0')}:${start.getMinutes().toString().padStart(2, '0')} - ${end.getHours().toString().padStart(2, '0')}:${end.getMinutes().toString().padStart(2, '0')}`;
		}else{
			return `${start.getFullYear()}年${start.getMonth() + 1}月${start.getDate()}日 ${start.getHours().toString().padStart(2, '0')}:${start.getMinutes().toString().padStart(2, '0')} - ${end.getFullYear()}年${end.getMonth() + 1}月${end.getDate()}日 ${end.getHours().toString().padStart(2, '0')}:${end.getMinutes().toString().padStart(2, '0')}`;
		}
	}
}

const showToast = ({title,icon,mask,duration}) => {
	uni.showToast({
		title,
		icon,
		mask,
		duration
	})
}

export {
	getBcmDictName,
	getBcmDictList,
	getBcmDictLists,
	getBcmDictItem,
	formatPrice,
	showToast,
	formatTimeRange
}